layui.define(['layer', 'form', 'jquery', 'element'], function (exports) {
  var $ = layui.$,
      layer = layui.layer;

  var accountInfo = {
    init: function () {
      $('.acco').addClass('on');
    },
    loadData: function () {
      $.getJSON('/account/getBaseInfo', function (result) {
        if (result.code == 0) {
          var data = result.data;
          $(':text').each(function (index, element) {
            var attr_name = $(element).attr('name');
            $(element).val(data[attr_name]);
          });
          if(data.mobile){
            $('.bind_mobile').html(data.mobile);
          }
          $(".nickName").html(data.nickName);
          $(".bankAccountName").html(data.bankAccountName);
        } else {
          if (result.message == 100101) {
            $('.con11-r .nav ul li').eq(1).click();
          }
        }
      })

      $.getJSON('/account/getRealNameAuthInfo', function (result) {
        if (result.success) {
          var realNameAuth = result.data;
          if (realNameAuth) {
            $('.choose').hide();
            if (realNameAuth.authType == 'person') {
              $('.name').append(realNameAuth.authName);
              $('.card-num').append(realNameAuth.documetNo);
              if(realNameAuth.longTermEffective){
                $('.term').append('长期有效');
              }else {
                $('.term').append(realNameAuth.invalidDate);
              }
              $('.cert-copy').show();
            } else {
              $('.name').append(realNameAuth.authName);
              $('.legalname').append(realNameAuth.legalName);
              $('.unicode').append(realNameAuth.documetNo);
              $('.comp-copy').show();

            }
          }

        }
      })

    },

    bindEvent: function () {

      $("#cho-type li a").on("click", function () {
        $(this).parent().addClass("on").siblings().removeClass("on");
        $("#form-set dl").eq($(this).parent().index()).show().siblings().hide();
        return false;
      })

      //修改昵称弹窗
      $("#renickname").on("click", function () {
        layer.open({
          type: 1,
          title: false,
          closeBtn: 0,
          btn: ['确定', '取消'],
          btnAlign: 'c',
          skin: 'pn',
          area: ['500px', '260px'],
          content: $('.pop-nickname'),
          yes: function (index, layero) {
            var nickName = layero.find('input').val();
            var index = layer.load(1, {shade: [0.1, '#fff']});
            $.ajax({
              url: '/account/updateBaseInfo',
              type: 'post',
              data: {nickName: nickName},
              success: function (result) {
                layer.closeAll();
                if (result.code == 0) {
                  $('.must-info .nickName').html(nickName);
                  layer.close(index);
                } else {
                  layer.msg(result.message)
                }
              }
            })

          },
          btn2: function (index, layero) {
            //【按钮二】的回调
          },
          success: function (layero, index) {
            $('.pop-nickname .head a').on('click', function () {
              layer.close(index);
              return false;
            })
          }
        });
        return false;
      })

      //修改密码弹窗
      $("#repass").on("click", function () {
        $('.pop-pass form input').val('');
        layer.open({
          type: 1,
          title: false,
          closeBtn: 0,
          btn: ['确定', '取消'],
          btnAlign: 'c',
          skin: 'pp',
          area: ['560px', '398px'],
          content: $('.pop-pass'),
          yes: function (index, layero) {
            var password = layero.find('input[name="pwd"]').val(),
                newPassword = layero.find('input[name="password"]').val(),
                confirmPassword = layero.find('input[name="confirmPassword"]').val();
            if(newPassword.length<6){
              layer.msg('新密码长度不小于6位');
              return false;
            }
            if(newPassword!=confirmPassword){
              layer.msg('新密码和确认密码不一致');
              return false
            }
            var index = layer.load(1, {shade: [0.1, '#fff']});
            $.post('/account/updatePassword',{password:password,newPassword:newPassword},function (result) {
              layer.close(index);
              if(!result.success) {
                layer.msg(result.message)
              }else{
                layer.msg('修改成功', function(){
                  layer.closeAll();
                });
              }
            },'json')

          },
          btn2: function (index, layero) {
            //【按钮二】的回调
          },
          success: function (layero, index) {
            $('.pop-pass .head a').on('click', function () {
              layer.close(index);
              return false;
            })
          }
        });
        return false;
      })

      $('.nav ul li').click(function () {
        $(this).addClass('on').siblings().removeClass('on');
        $('.tab_container .content').eq(
            $(this).index()).show().siblings().hide();
      })

      $('#account_form').validate({

        errorElement: 'em',
        submitHandler: function (form) {
          var formData = $(form).serializeJSON();
          $.ajax({
            url: '/account/updateBaseInfo',
            type: 'post',
            data: formData,
            success: function (result) {
              if (result.code == 0) {
                layer.msg('保存成功！')
              } else {
                layer.msg(result.message);
              }
            }
          })
        }
      })
    }

  }
  accountInfo.init();
  accountInfo.loadData();
  accountInfo.bindEvent();
  exports('accountInfo', {});
})